package com.example.tliasfight.controller;

import com.example.tliasfight.pojo.Emp;
import com.example.tliasfight.pojo.Result;
import com.example.tliasfight.service.LoginService;
import com.example.tliasfight.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController
{
    @Autowired
    private LoginService loginService;
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp)
    {
        Emp e=loginService.login(emp);
        if(e!=null)//登录成功,数据库确实有这样的用户名和与之对应的密码
        {
            Map<String, Object> claims=new HashMap();
            claims.put("id",e.getId());
            claims.put("username",e.getUsername());
            claims.put("name",e.getName());
            //以上不就是构造了类似于json的结构传给JWT，然后claims是封装了map的，可以当map<String,Object>用
            String jwt=JwtUtils.generateJwt(claims);//jwt就包含了当前登录的员工的id，username，name这三个信息

            return Result.success(jwt);
        }
       return Result.error("用户名或密码错误");
    }
}
